import os
import requests
import pytest


# 钉钉机器人 WebHook（换成自己的）
DING_WEBHOOK = "https://zd-openplatform.bigdatacq.com/robot/send?access_token=72213b3c6a2b3a794ea0c34ae3beef27b118437cba6f3ddca07a0a5f3ce2a205"


# ------------ 钉钉 ------------
def ding(msg: str):
    if not DING_WEBHOOK:
        return
    data = {"msgtype": "text", "text": {"content": msg}}
    res = requests.post(DING_WEBHOOK, json=data, timeout=5)


@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
    """捕获每个用例的详细执行结果"""
    outcome = yield
    report = outcome.get_result()
    import inspect
    for name, value in inspect.getmembers(item):
        if not name.startswith('__'):
            print(f"{name} = {value}")
    exit()
    # 只处理“call”阶段（用例主体）
    if report.when == "call":
        # 失败时记录
        if report.failed:
            summary = (
                f"告警:\n"
                f"  {item.function.__doc__} 报错: {report.longreprtext}\n"
                f"  {item.nodeid} 耗时：{int(report.duration)} 秒\n"
            )
            ding(summary)

